package com.ht.shop.controller;

import com.ht.shop.annotations.RequestMapping;
import com.ht.shop.pojo.TbAddress;
import com.ht.shop.pojo.TbUser;
import com.ht.shop.service.AddressService;
import com.ht.shop.service.impl.AddressServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
//address
@RestController
@RequestMapping("/address")
public class AddressController {
    private AddressService addressService = new AddressServiceImpl();
    @Autowired
    public void setAddressService(AddressService addressService) {
        this.addressService = addressService;
    }

    @RequestMapping("/addaddress")
    public String addAddress(HttpServletRequest request, HttpServletResponse response) {
        TbUser loginUser = (TbUser) request.getSession().getAttribute("loginUser");
        Long uid = loginUser.getId();
        String aname = request.getParameter("aname");
        String aphone = request.getParameter("aphone");
        String adetail = request.getParameter("adetail");
        TbAddress tbAddress = new TbAddress();
        tbAddress.setName(aname);
        tbAddress.setPhone(aphone);
        tbAddress.setDetail(adetail);
        tbAddress.setUid(uid);
        addressService.addAddress(tbAddress.getDetail(),tbAddress.getName(),tbAddress.getPhone(),tbAddress.getUid());
        try {
            List<TbAddress> tbAddressList = (List<TbAddress>) request.getSession().getAttribute("list");

            tbAddressList.clear();
            List<TbAddress> allAddress = addressService.findAllAddress();

            tbAddressList.addAll(allAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        response.setContentType("text/html;charset=utf-8");
//        return "添加成功";
        return "forward:/self_info.jsp";
    }
    @RequestMapping("/show")
    public String show(HttpServletRequest request,HttpServletResponse response) {

        TbUser loginUser = (TbUser) request.getSession().getAttribute("loginUser");
        Long uid = loginUser.getId();
        List<TbAddress> allAddressByUid = addressService.findAllAddressByUid(uid);
        request.getSession().setAttribute("list",allAddressByUid);

        return "forward:/self_info.jsp";

    }
    //updateAddress
    @RequestMapping("/updateAddress")
    public String update(HttpServletRequest request,HttpServletResponse response){
        String aname = request.getParameter("aname");
        String aphone = request.getParameter("aphone");
        String adetail = request.getParameter("adetail");
//        System.out.println(request.getParameter("aaid"));
        long aid = Long.parseLong(request.getParameter("aaid"));

        System.out.println("--------"+aid);
        TbAddress tbAddress = new TbAddress();

        tbAddress.setName(aname);
        tbAddress.setPhone(aphone);
        tbAddress.setDetail(adetail);
        tbAddress.setId(aid);
//        System.out.println("=================="+tbAddress);
        request.getSession().setAttribute("addressList",tbAddress);

        return "forward:/updateAddress.jsp";


    }
    @RequestMapping("/update")
    public String updateAddress(HttpServletRequest request,HttpServletResponse response){

        String aname = request.getParameter("aname");
        String aphone = request.getParameter("aphone");
        String adetail = request.getParameter("adetail");
        long aid = Long.parseLong(request.getParameter("aid"));
        TbAddress tbAddress = new TbAddress();
        tbAddress.setName(aname);
        tbAddress.setPhone(aphone);
        tbAddress.setDetail(adetail);
        tbAddress.setId(aid);
        addressService.updateAddress(tbAddress.getDetail(),tbAddress.getName(),tbAddress.getPhone(),tbAddress.getId());

        try {
            List<TbAddress> tbAddressList = (List<TbAddress>) request.getSession().getAttribute("list");
            tbAddressList.clear();
            List<TbAddress> allAddress = addressService.findAllAddress();
            tbAddressList.addAll(allAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "forward:/self_info.jsp";


    }
    @RequestMapping("/delete")
    public  String deleteAddress(HttpServletRequest request,HttpServletResponse response){
        long aid = Long.parseLong(request.getParameter("aid"));
        System.out.println(aid);
        TbAddress tbAddress = new TbAddress();
        tbAddress.setId(aid);
        addressService.deleteAddressById(tbAddress.getId());

        try {
            List<TbAddress> tbAddressList = (List<TbAddress>) request.getSession().getAttribute("list");
            tbAddressList.clear();
            List<TbAddress> allAddress = addressService.findAllAddress();
            tbAddressList.addAll(allAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "forward:/self_info.jsp";
    }
}
